package com.dankegongyu.lib.common.widget.gestureLock;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;

import com.dankegongyu.lib.common.util.ScreenUtil;


/**
 */
public class StyleLockView extends GestureLockView {

    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

    private int mCenterX, mCenterY;
    private int mWidth, mHeight;

    private int mRadius;
    private int mRadiusSelected;
    private int mStrokeWidth = 4;

    private int COLOR_NORMAL = 0xFFcad6d5;
    private int COLOR_SELECTED = 0xFF49bec6;
    private int COLOR_SELECTED_ALPHA = 0xFFebf8f9;
    private int COLOR_ERROR = 0xFFFF0000;
    private int COLOR_ERROR_ALPHA = 0x99FF0000;


    private float middleRate = 0.8F;
    private float arrowRate = 0.3F;
    private float arrowDistanceRate = 0.65F;
    private int arrowDistance;

    private RectF middleOval;

    private Path arrow;

    public StyleLockView(Context context) {
        this(context, null);
    }

    public StyleLockView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public StyleLockView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        arrow = new Path();
        middleOval = new RectF();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        mWidth = w;
        mHeight = h;

        mCenterX = mWidth / 2;
        mCenterY = mHeight / 2;

//        mRadius = mCenterX > mCenterY ? mCenterY : mCenterX;
        mRadius = 10;
        mRadiusSelected = 50;

        float r = mRadius * middleRate;
        middleOval.left = mCenterX - r;
        middleOval.right = mCenterX + r;
        middleOval.top = mCenterY - r;
        middleOval.bottom = mCenterY + r;

        arrowDistance = (int) (mRadius * arrowDistanceRate);

        int length = (int) (mRadius * arrowRate);
        arrow.reset();
        arrow.moveTo(mCenterX - length, mCenterY + length - arrowDistance);
        arrow.lineTo(mCenterX, mCenterY - arrowDistance);
        arrow.lineTo(mCenterX + length, mCenterY + length - arrowDistance);
        arrow.close();
    }

    @Override
    protected void doDraw(LockerState state, Canvas canvas) {
        switch (state) {
            case LOCKER_STATE_NORMAL:
                mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
                mPaint.setStrokeWidth(mStrokeWidth);
                mPaint.setColor(COLOR_NORMAL);
                canvas.drawCircle(mCenterX, mCenterY, mRadius, mPaint);
                break;
            case LOCKER_STATE_SELECTED:
                mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
                mPaint.setColor(COLOR_SELECTED);
                mPaint.setStrokeWidth(mStrokeWidth);
                canvas.drawCircle(mCenterX, mCenterY, mRadiusSelected, mPaint);

                mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
                mPaint.setStrokeWidth(mStrokeWidth);
                mPaint.setColor(COLOR_SELECTED_ALPHA);
                canvas.drawCircle(mCenterX, mCenterY, mRadiusSelected - mStrokeWidth, mPaint);

                mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
                mPaint.setStrokeWidth(mStrokeWidth);
                mPaint.setColor(COLOR_SELECTED);
                canvas.drawCircle(mCenterX, mCenterY, mRadius, mPaint);
                break;
            case LOCKER_STATE_ERROR:
                mPaint.setStyle(Paint.Style.STROKE);
                mPaint.setColor(COLOR_ERROR);
                mPaint.setStrokeWidth(mStrokeWidth);
                canvas.drawCircle(mCenterX, mCenterY, mRadiusSelected, mPaint);

                mPaint.setStyle(Paint.Style.FILL);
                mPaint.setStrokeWidth(mStrokeWidth);
                mPaint.setColor(COLOR_ERROR_ALPHA);
                canvas.drawCircle(mCenterX, mCenterY, mRadiusSelected - mStrokeWidth, mPaint);

                mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
                mPaint.setStrokeWidth(mStrokeWidth);
                mPaint.setColor(COLOR_ERROR);
                canvas.drawCircle(mCenterX, mCenterY, mRadius, mPaint);
                break;
        }
    }

    @Override
    protected void doArrowDraw(Canvas canvas) {
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(COLOR_ERROR);
        canvas.drawPath(arrow, mPaint);
    }

    public void setRadius(int r) {
        mRadius = ScreenUtil.dp2px(r);
    }

    public void setRadiusSelected(int r) {
        mRadiusSelected = ScreenUtil.dp2px(r);
    }

    public void setStrokeWidth(int r) {
        mStrokeWidth = ScreenUtil.dp2px(r);
    }

    public void setColorNormal(int res) {
        COLOR_NORMAL = res;
    }

    public void setColorSelected(int res) {
        COLOR_SELECTED = res;
    }

    public void setColorSelectedAlpha(int res) {
        COLOR_SELECTED_ALPHA = res;
    }

    public void setColorError(int res) {
        COLOR_ERROR = res;
    }

    public void setColorErrorAlpha(int res) {
        COLOR_ERROR_ALPHA = res;
    }

}
